# Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


# Folders and files
SRC_DIR=src
INC_DIR=include
RELEASE_DIR=release
DEBUG_DIR=debug
DOC_DIR=doc
DOXYFILE=etc/doxygen_configuration.ini

# Commands
CMAKE=cmake -Wdev

# Package and archive
PACKAGE=PlanetWars2dRT-SDL2
FILES_TO_BUILD=$(SRC_DIR)/ $(INC_DIR)/ CMakeLists.txt
FILES_TO_ARCHIVE=$(FILES_TO_BUILD) etc/ makefile \
	licenses/ *.md \
	.gitignore .editorconfig .dir-locals.el \
	.gitlab-ci.yml .travis.yml


default: bin-release

all: bin-release doc default-archive


.PHONY: \
	default all \
	packages \
	tests tests-cppcheck tests-vera++ \
	bin bin-release bin-debug \
	static-libs SDL2_static SDL2_gfx_static SDL2_image_static \
	externals/core externals/SDL2_gfx externals/SDL2 \
	read-doc-web read-doc-pdf doc doc-web doc-pdf \
	see-todo-comments \
	archives default-archive zip tar-gz tar-bz2 tar-xz 7z \
	clean clean-build clean-build-release clean-build-debug \
	clean-bin clean-externals \
	clean-python clean-profiling \
	clean-ide clean-qt-creator clean-codeblocks \
	clean-tmp clean-doc clean-latex clean-archives clean-git


packages: $(RELEASE_DIR)/Makefile
	cd $(RELEASE_DIR) && make package


tests: bin-release tests-cppcheck tests-vera++

tests-cppcheck:
	cppcheck \
		--enable=warning,style --verbose --quiet --error-exitcode=1 \
		$(SRC_DIR) $(INC_DIR)

tests-vera++:
	@chmod +x ./tools/vera++-tests.sh
	./tools/vera++-tests.sh $(SRC_DIR) $(INC_DIR)


bin: bin-release bin-debug

bin-release: $(RELEASE_DIR)/Makefile
	cd $(RELEASE_DIR) && make

$(RELEASE_DIR)/Makefile: $(FILES_TO_BUILD)
	@mkdir -p $(RELEASE_DIR)
	cd $(RELEASE_DIR) && $(CMAKE) -DCMAKE_BUILD_TYPE=release ..

bin-debug: $(DEBUG_DIR)/Makefile
	cd $(DEBUG_DIR) && make

$(DEBUG_DIR)/Makefile: $(FILES_TO_BUILD)
	@mkdir -p $(DEBUG_DIR)
	cd $(DEBUG_DIR) && $(CMAKE) -DCMAKE_BUILD_TYPE=debug ..


static-libs: SDL2_static SDL2_gfx_static SDL2_image_static

SDL2_image_static: externals/SDL2_image
	cd externals/ && chmod +x ./*.sh && ./get_compiled_SDL2_image.sh

SDL2_gfx_static: externals/SDL2_gfx
	cd externals/ && chmod +x ./*.sh && ./get_compiled_SDL2_gfx.sh

SDL2_static: externals/SDL2
	cd externals/SDL2 && mkdir -p build && cd build && cmake .. && make SDL2-static


externals/core:
	git submodule init   externals/core
	git submodule update externals/core

externals/SDL2_image:
	chmod +x externals/*.sh
	cd externals && ./get_SDL2_image.sh

externals/SDL2_gfx:
	chmod +x externals/*.sh
	cd externals && ./get_SDL2_gfx.sh

externals/SDL2:
	chmod +x externals/*.sh
	cd externals && ./get_SDL2.sh
	cd externals/SDL2/include/ && ln -s . SDL2


read-doc-web: doc-web
	xdg-open $(DOC_DIR)/html/index.html &

read-doc-pdf: doc-pdf
	xdg-open $(DOC_DIR)/latex/refman.pdf &

doc: doc-web doc-pdf

doc-pdf: doc-web
	cd $(DOC_DIR)/latex/ && make

doc-web: $(INC_DIR) $(DOXYFILE)
	doxygen $(DOXYFILE)


see-todo-comments:
	@grep --color=auto -rn TODO src/


archives: zip tar-gz tar-bz2 tar-xz 7z

default-archive: tar-xz

zip: $(PACKAGE).zip

$(PACKAGE).zip: $(FILES_TO_ARCHIVE)
	zip $(PACKAGE).zip -r -- $(FILES_TO_ARCHIVE)

tar-gz: $(PACKAGE).tar.gz

$(PACKAGE).tar.gz: $(FILES_TO_ARCHIVE)
	tar -zcvf $(PACKAGE).tar.gz -- $(FILES_TO_ARCHIVE)

tar-bz2: $(PACKAGE).tar.bz2

$(PACKAGE).tar.bz2: $(FILES_TO_ARCHIVE)
	tar -jcvf $(PACKAGE).tar.bz2 -- $(FILES_TO_ARCHIVE)

tar-xz: $(PACKAGE).tar.xz

$(PACKAGE).tar.xz: $(FILES_TO_ARCHIVE)
	tar -cJvf $(PACKAGE).tar.xz -- $(FILES_TO_ARCHIVE)

7z: $(PACKAGE).7z

$(PACKAGE).7z: $(FILES_TO_ARCHIVE)
	7z a -t7z $(PACKAGE).7z $(FILES_TO_ARCHIVE)


clean: \
	clean-build clean-externals clean-bin \
	clean-python clean-profiling clean-ide \
	clean-tmp clean-doc clean-latex clean-archives

clean-build: clean-build-release clean-build-debug
	@$(RM) -rf -- build/ Build/ builds/ Builds/

clean-build-release:
	@$(RM) -rf -- release/ Release/ $(RELEASE_DIR)/

clean-build-debug:
	@$(RM) -rf -- dbg/ debug/ Debug/ $(DEBUG_DIR)/

clean-externals:
	$(RM) -rf -- externals/SDL2/ externals/SDL2_gfx/

clean-bin:
	$(RM) -rf -- *.o *.a *.so *.ko *.lo *.dll *.out

clean-python:
	$(RM) -rf -- *.pyc __pycache__

clean-profiling:
	$(RM) -rf -- callgrind.out.*

clean-ide: clean-qt-creator clean-codeblocks

clean-qt-creator:
	$(RM) -f -- qmake_makefile *.pro.user

clean-codeblocks:
	$(RM) -f -- *.cbp

clean-tmp:
	$(RM) -r -- \
		*~ *.bak *.backup .\#* \#* *.sav *.save *.autosav *.autosave \
		*.log *.log.* error_log* \
		.cache/ .thumbnails/

clean-doc:
	@$(RM) -rf -- doc/ Doc/ docs/ Docs/ documentation/ Documentation/

clean-latex:
	$(RM) -f -- \
		*.pdf *.dvi *.ps \
		*.acn *.aux *.bcf *.cut *.fls *.glo *.ist *.lof *.nav \
		*.run.xml *.snm *.toc *.vrb *.vrm *.xdy \
		*.fdb_latexmk *-converted-to.* \
		*.synctex *.synctex.gz *.synctex.bz2 *.synctex.xz \
		*.synctex.zip *.synctex.rar

clean-archives:
	$(RM) -rf -- \
		*.deb *.rpm *.exe *.msi *.dmg *.apk *.ipa \
		*.zip *.tar.* *.tgz *.7z *.gz *.bz2 *.lz *.lzma *.xz *.rar *.jar

clean-git:
	git clean -fdx
